// pages/apps/user/address/address4ShippingEdit/address4ShippingEdit.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: getApp().globalData.IMG_URL,
    token: "",
    showPage: false,
    latitude: 0,
    longitude: 0,
    addrBean: {} as any,
    isAddEvent: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('pageData', function (param) {
      const addrBean = param.bean
      if (addrBean != null) {
        that.setData({ addrBean: addrBean })
        wx.setNavigationBarTitle({ title: "修改地址" })
        that.setData({ isAddEvent: false });
      } else {
        wx.setNavigationBarTitle({ title: "新增地址" })
        that.setData({ isAddEvent: true });
      }

      that.setData({ showPage: true });
    })
    const token = wx.getStorageSync('token')
    this.setData({ token: token });


    // 获取当前位置
    this.getLocation()
  },
  radioChange(param: any) {
    const act = param.currentTarget.dataset.act
    const hasDefault = 'Y' == act ? 'N' : 'Y'
    this.setData({ 'addrBean.hasDefault': hasDefault })
  },
  bindNicknameInput(e: any) {
    this.setData({ 'addrBean.nickname': e.detail.value })
  },
  bindPhoneInput(e: any) {
    this.setData({ 'addrBean.phone': e.detail.value })
  },
  bindAddrNameInput(e: any) {
    this.setData({ 'addrBean.addrName': e.detail.value })
  },
  bindAddressInput(e: any) {
    this.setData({ 'addrBean.address': e.detail.value })
  },
  submit() {
    const that = this;
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "/fc/emall/shippingAddress/addOrEdit"

    if (this.data.addrBean.nickname == null || this.data.addrBean.nickname.trim().length == 0) {
      wx.showToast({ title: '请填写收货人' })
      return
    }

    if (this.data.addrBean.phone == null || this.data.addrBean.phone.trim().length == 0) {
      wx.showToast({ title: '请填写手机号码' })
      return
    }

    if (this.data.addrBean.address == null || this.data.addrBean.address.trim().length == 0) {
      wx.showToast({ title: '请选择位置' })
      return
    }

    wx.showLoading({ title: '正在提交' })
    wx.request({
      url: url,
      method: 'POST',
      data: this.data.addrBean,
      header: {
        'X-Access-Token': that.data.token,
        'Tenant-Id': '-1'
      },
      success(res: any) {
        if (res.data.code == 200) {
          // console.error("结果: " + JSON.stringify(res.data))
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({})
        } else {
          console.info(JSON.stringify(res))
        }
      },
      complete(_) {
        wx.hideLoading()
      }
    })
  },
  // 定位
  getLocation() {
    var that = this
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
      success(res) {
        // console.error("getLocation-success:" + JSON.stringify(res))
        const lat = res.latitude
        const lng = res.longitude
        that.setData({ latitude: lat, longitude: lng })
      },
      fail(res) {
        console.error("getLocation-fail:" + JSON.stringify(res))
      }
    })
  },
  // 选择兴趣点
  goChooseLocation() {
    var that = this
    wx.chooseLocation({
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      success: function (res) {
        console.error("chooseLocation:success: " + JSON.stringify(res))
        const addrBean = that.data.addrBean
        addrBean.addrName = res.name
        addrBean.address = res.address
        addrBean.latitude = res.latitude
        addrBean.longitude = res.longitude
        that.setData({ addrBean: addrBean })
      },
      fail: function (res) {
        console.error("chooseLocation:fail: " + JSON.stringify(res))
      },
      complete: function (res) {
      },
    })
  },
  // // 选择兴趣点
  // goChooseAddress() {
  //   var that = this
  //   wx.chooseAddress({
  //     success(res) {
  //       console.error("chooseAddress->" + JSON.stringify(res))
  //       const addrBean = that.data.addrBean
  //       addrBean.nickname = res.userName
  //       addrBean.phone = res.telNumber
  //       addrBean.addrName = res.detailInfo

  //       addrBean.address = (res.provinceName != null ? res.provinceName : "") +
  //         (res.cityName != null ? res.cityName : "") +
  //         (res.countyName != null ? res.countyName : "")

  //       that.setData({ addrBean: addrBean })

  //       // console.log(res.postalCode)
  //       // console.log(res.provinceName)
  //       // console.log(res.cityName)
  //       // console.log(res.countyName)
  //       // console.log(res.detailInfo)
  //       // console.log(res.nationalCode)
  //       // console.log(res.telNumber)
  //     }
  //   })
  // },

  /**
   * 
   * @param param 删除收货地址
   */
  delAddr(param: any) {
    const id = param.currentTarget.dataset.id
    console.error(id)
    const that = this
    wx.showModal({
      title: '删除提示',
      content: '确定删除当前收货地址吗？',
      success(res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          that.exeDelAddr(id)
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  },
  exeDelAddr(id: string) {
    const that = this;
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "/fc/emall/shippingAddress/delete"

    wx.showLoading({ title: '正在删除' })
    wx.request({
      url: url,
      method: 'DELETE',
      data: {id:id},
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'X-Access-Token': that.data.token,
        'Tenant-Id': '-1'
      },
      success(res: any) {
        console.error("结果: " + JSON.stringify(res))
        if (res.data.code == 200) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({})
        } else {
          console.info(JSON.stringify(res))
        }
      },
      complete(_) {
        wx.hideLoading()
      }
    })
  }
})